<?php
Prado::using('Application.common.common');
class EditUser extends TPage
{
	public function onLoad($param)
	{
		parent::onLoad($param);
		if (!$this->IsPostBack)
		{
			$this->Master->pageHead->Title = "Admin | Edit User";
			$this->AdminMenu->lnkManageUser->CssClass = "active";
			
			if ($this->Request->contains('m')) {
				$this->Notice->Type = UserNoticeType::NoticeAdmin;
				if ($this->Request['m'] == 'cu')
					$this->Notice->Text = $this->Application->getModule('message')->translate('ADD_SUCCESS','User','');
				else if ($this->Request['m'] == 'uu')	
					$this->Notice->Text = $this->Application->getModule('message')->translate('UPDATE_SUCCESS','User','');
				
			}
			
			// Populate Age Group List
			$this->cboAgeSelector->DataSource = AgeGroupRecord::finder()->getAllItems();
			$this->cboAgeSelector->DataBind();
			
			// Populate Cuisine List
			$this->cboCuisineSelector->DataSource = CuisineRecord::finder()->getAllItems();
			$this->cboCuisineSelector->DataBind();
			
			$this->txtUserID->Value = ($this->Request->contains('uid')) ? TPropertyValue::ensureInteger($this->Request['uid']) : 0;	
			$userRecord = UserRecord::finder()->findByPk($this->txtUserID->Value);
			if ($userRecord instanceof UserRecord)
			{
				$this->txtUserName->Text=$userRecord->UserName;
				$this->txtEmail->Text=$userRecord->Email;
				
				$this->cboAgeSelector->SelectedValue = $userRecord->AgeGroup;	
				$this->cboCuisineSelector->SelectedValue = $userRecord->PreferredCuisine;	
				if ($userRecord->Status == 2)
					$this->chkAdmin->Checked = true;				
			}
		}
	}
	
	protected function btnSubmit_Clicked($sender, $param)
	{
		if ($this->IsValid)
		{						
			try {					
				$userRecord = UserRecord::finder()->findByPk($this->txtUserID->Value);
				$userRecord->UserName = $this->txtUserName->SafeText;
				$userRecord->Email = $this->txtEmail->SafeText;	
				$userRecord->AgeGroup = $this->cboAgeSelector->SelectedValue;
				$userRecord->PreferredCuisine = $this->cboCuisineSelector->SelectedValue;
				$userRecord->Status = "1";
				if ($this->chkAdmin->Checked)	
					$userRecord->Status = "2";
				$userRecord->ModifyBy = $this->Application->User->ID;
				$userRecord->ModifyDate = time();										
				$userRecord->save();						
				$this->Response->redirect($this->Service->ConstructUrl('admin.EditUser', array('uid'=>$userRecord->ID, 'm'=>'uu')));			
			}
			catch(TException $e)
			{
				$this->Notice->Type = UserNoticeType::Error;
				$this->Notice->Text = $e;
			}
		}
	}
	
	protected function btnCancel_Clicked($sender, $param)
	{
		$this->Response->redirect($this->Service->ConstructUrl("admin.ManageUsers"));				
	}
	
	protected function uniqueCheck_ServerValidated($sender, $param)
	{
		if ($param->Value != '')
		{
			$criteria = new TActiveRecordCriteria;
			$criteria->Condition = "user_email = :email and user_id <> " . $this->txtUserID->Value;
			$criteria->Parameters[":email"] = $param->Value;
			$param->IsValid = !(UserRecord::finder()->find($criteria) instanceof UserRecord);
		}
	}
}

?>